﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FzWeb.Filter;
using FzWeb.Models;

namespace FzWeb.Controllers
{

    public class HomeController : Controller
    {

        private BaseDataService baseDataService = new BaseDataService();
        private OpinionService opinionService = new OpinionService();
        private SelectControlService selectControlService = new SelectControlService();
        public ActionResult Index()
        {

            ViewData["adList"] = baseDataService.GetAdList();
            ViewData["professorList"] = baseDataService.GetNewsList(20).Take(2).ToList();
            ViewData["IndustryList"] = new SelectList(selectControlService.GetCompanyIndustry(),"key","value");
            ViewData["postdateList"] = new SelectList(selectControlService.GetPostDate(), "key", "value");
            ViewData["news"] = baseDataService.GetNewsList();
            return View();
        }

        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <param name="classid">新闻类别id</param>
        /// <returns></returns>
        public ActionResult NewsList(int classid, int count)
        {
            return View(baseDataService.GetNewsList(c => c.News_Class == baseDataService.GetNewsClass(classid), count));
        }

        public ActionResult NewsList2(int classid, int count)
        {
            IList<News> newslist = baseDataService.GetNewsList(c => c.News_Class.Id == classid, count);
            return View(newslist);
            //return View(baseDataService.GetNewsList(c => c.News_Class.Id ==classid, count));
        }


        public ActionResult NewsListIndex(int[] classid)
        {
            IList<News> list = new List<News>();
            foreach(var i in classid)
            {
                var ll = baseDataService.GetNewsList(c => c.News_Class.Id == i);
                foreach (var l in ll)
                {
                    list.Add(l);
                }
                
            }
            return View(list.Take(14));
        }

        /// <summary>
        /// 0-hot 1-new
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Top10(int id)
        {
            return View(baseDataService.GetTop10(id));
        }

        public ActionResult Top20(int id)
        {
            return View(baseDataService.GetTop20(id));
        }
        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <param name="classid">新闻类别id</param>
        /// <returns></returns>
        public ActionResult NewsList5(int classid)
        {
            return View(baseDataService.GetNewsList(c => c.News_Class == baseDataService.GetNewsClass(classid), 5));
        }

        /// <summary>
        /// 新闻明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult NewsDetail(int id)
        {
            return View(baseDataService.GetNews(id));
        }

        #region 资讯中心
        /// <summary>
        /// 资讯中心
        /// </summary>
        /// <returns></returns>
        public ActionResult InformationCenter()
        {
            return View(baseDataService.GetNewsClassAll(22));
        }

        public ActionResult InformationList(int classId, int? page)
        {
            IList<News> list;
            int p = 1;
            if (page.HasValue)
            {
                p = (int)page;
            }
            list = baseDataService.GetNewsList(c => c.News_Class == baseDataService.GetNewsClass(classId)).Skip((p - 1) * 20).Take(20).ToList();

            ViewData["pageIndex"] = p;
            var count = baseDataService.GetNewsList(c => c.News_Class == baseDataService.GetNewsClass(classId)).Count;
            ViewData["count"] = count;
            ViewData["pageCount"] = (count / 20) + 1;

            ViewData["class"] = baseDataService.GetNewsClass(classId).Name;
            return View(list);
        }

        public ActionResult InformationDetail(int id)
        {
            ViewData["lastNews"] = baseDataService.GetLastNews(id);
            ViewData["afterNews"] = baseDataService.GetAfterNews(id);
            News n = baseDataService.GetNews(id);
            if (n.ViewNo == null) n.ViewNo = 1;
            n.ViewNo++;
            // ViewData["ViewNo"] = n.ViewNo;
            baseDataService.UpdateNews(n);
            return View(n);
        }
        #endregion

        #region 公司业务
        public ActionResult BusinessCenter()
        {
            return View(baseDataService.GetNewsList());
        }

        public ActionResult BusinessList(int classId, int? showId)
        {
            ViewData["list"] = baseDataService.GetNewsList(classId);
            News news = new News();
            if (showId.HasValue)
            {
                news = baseDataService.GetNews(Convert.ToInt32(showId));
            }
            else
            {
                news = baseDataService.GetNewsList(classId).First();
            }
            return View(news);
        }
        #endregion

        #region Q&A
        public ActionResult QuestionCenter(int? classId)
        {
            ViewData["list"] = baseDataService.GetNewsClass(32).News_Childs.ToList();
            IList<News> list;
            if (classId.HasValue)
            {
                list = baseDataService.GetNewsClass(Convert.ToInt32(classId)).News.ToList();
            }
            else
            {
                list = baseDataService.GetNewsClass(32).News_Childs.First().News.ToList();
            }
            return View(list);
        }

        public ActionResult QuestionDetail(int classId,int showId)
        {
            ViewData["list"] = baseDataService.GetNewsClass(32).News_Childs.ToList();
            return View(baseDataService.GetNews(showId));
        }
        #endregion

        /// <summary>
        /// 求职招聘
        /// </summary>
        public ActionResult Job()
        {
            return View();
        }

        /// <summary>
        /// 留言反馈
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageCenter(int? page)
        {
            IList<Opinion> list;
            int p = 1;
            if (page.HasValue)
            {
                p = (int)page;
            }
            list = opinionService.GetOpinionList().OrderByDescending(c=>c.SendTime).Skip((p - 1) * 20).Take(20).ToList();

            ViewData["pageIndex"] = p;
            var count = opinionService.GetOpinionList().Count;
            ViewData["count"] = count;
            ViewData["pageCount"] = (count / 20) + 1;
            return View(list);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MessageCenter(FormCollection form)
        {
            if (form["CheckCode"].Equals(HttpContext.Session["CheckCode"]))
            {
                Opinion o = new Opinion();
                TryUpdateModel<Opinion>(o);
                o.Content = form["opinionContent"];
                o.UserType = "游客";
                opinionService.CreateOpinion(o);
                TempData["opinionError"] = "";
            }
            else
            {
                TempData["opinionError"] = "错误";
            }
            return RedirectToAction("MessageCenter");
        }

        public ActionResult Professor(int? showId)
        {
            ViewData["list"] = baseDataService.GetNewsList(20);
            News prof = new News();
            if (showId.HasValue)
            {
                prof = baseDataService.GetNews(Convert.ToInt32(showId));
            }
            return View(prof);
        }

        public ActionResult Hr_Tool()
        {
            return View();
        }

        #region 政策法规
        public ActionResult LawCenter()
        {
            return View("InformationCenter",baseDataService.GetNewsClassAll(21));
        }
        #endregion
    }
}
